package org.loujiarong.yugioh.play;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.loujiarong.yugioh.gameplay.database.CardNameClassMappingDAO;
import org.loujiarong.yugioh.gameplay.database.DatabaseCoordinator;
import org.loujiarong.yugioh.gameplay.duel.Deck;
import org.loujiarong.yugioh.gameplay.duel.Duel;
import org.loujiarong.yugioh.gameplay.duel.ExtraDeck;
import org.loujiarong.yugioh.gameplay.duel.Hand;
import org.loujiarong.yugioh.gameplay.duel.MainDeck;
import org.loujiarong.yugioh.gameplay.duel.Player;
import org.loujiarong.yugioh.gameplay.duel.SideDeck;
import org.loujiarong.yugioh.gameplay.duel.SingleDuel;
import org.loujiarong.yugioh.util.Command;
import org.loujiarong.yugioh.util.GameConstants.CardPosition;
import org.loujiarong.yugioh.util.GameConstants.Phase;
import org.loujiarong.yugioh.gameplay.card.Card;
import org.loujiarong.yugioh.gameplay.card.DummyCard;
import org.loujiarong.yugioh.gameplay.card.Monster;

public class SimpleYugioh {

	/**
	 * @param args
	 */
	static Duel duel;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DatabaseCoordinator.initialize("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/yugioh", "anonymous", "");
		System.out.println("Welcome to Yugioh");
		System.out.print("[cmd] ");
		Scanner input = new Scanner(System.in);;
		String cmd;
		
		while(input.hasNext()) {			
			cmd = input.next();
			if (cmd.equals(Command.STARTDUEL)) {
				initDuel();
			}
			else if (duel != null) {
				if (cmd.endsWith(" " + Command.FIRSTTOGO)) {
					String[] para = cmd.split(" ");
					Player playerA = duel.getPlayerA();
					Player playerB = duel.getPlayerB();
					
					if(para[0].equals(Command.PLAYERA)) {
						duel.setTurnPlayer(playerA);						
					}
					else if(para[0].equals(Command.PLAYERB)) {
						duel.setTurnPlayer(playerA);	
					}
					else {
						System.err.println("wrong command format: " + cmd);
						System.out.print("[cmd] ");
						input = new Scanner(System.in);
						continue;
					}										
					playerA.draw(5);
					playerB.draw(5);
					duel = duel.duel();
					System.out.println("Duel ends. Winner is:" + duel.getWinner());
					duel = null;
				}
			}
			System.out.print("[cmd] ");
		}
	}
	

	
	public static void initDuel() {
	   duel = new SingleDuel(); 
	   try {
		   duel.getPlayerA().loadDeck(new File("D:\\tmp.txt"));
		   duel.getPlayerB().loadDeck(new File("D:\\tmp.txt"));
		   print(duel);
	   } catch (Exception ex) {
		   ex.printStackTrace();
	   }	   
	}
	
	public static void print(Duel duel) {   
	   print(duel.getPlayerA());
	   print(duel.getPlayerB());
	}
	
	public static void print(Player player) {
	   Hand playerHand = player.getHand();
	   System.out.println(player + ": ");
	   System.out.println("LP: " + player.getLifePoint());
	   System.out.println("手牌:");	   
	   for(Card card : playerHand.getCards()) {
		   System.out.println("\t<" + card + ">");
	   }
	   System.out.println("卡组 : " + player.getMainDeck().getCards().size());
	   System.out.println("额外 : " + player.getExtraDeck().getCards().size());
	   System.out.println("墓地 : " + player.getGraveYard().getCards().size());
	   System.out.println("除外区 : " + player.getRemovedFromPlayArea().getCards().size());
	   
	   System.out.println("怪兽区 : ");
	   int pos = 6;
	   for(Card card : player.getField().getMonsterZone()) {
		   System.out.print("\t" + pos + " ");
		   if(card instanceof DummyCard){
			   System.out.println("<>");
		   }
		   else if(card.getPos() == CardPosition.FACE_DOWN_ATTACK || card.getPos() == CardPosition.FACE_DOWN_DEFENCE ) {
			   System.out.println("<??>");
		   }
		   else {
			   if(card.getPos() == CardPosition.FACE_UP_ATTACK) {
				   System.out.println("<表攻|" + card + " " + ((Monster)card).getAtk() + " " + ((Monster)card).getDef() + ">");
			   }
			   else if(card.getPos() == CardPosition.FACE_UP_DEFENCE) {
				   System.out.println("<表防|" + card + " " + ((Monster)card).getAtk() + " " + ((Monster)card).getDef() + ">");
			   }
		   }
		   ++pos;
	   }	   
	   System.out.print("魔陷区 : ");
	   for(Card card : player.getField().getMonsterZone()) {
		   
		   if(card instanceof DummyCard){
			   System.out.print("<> ");
		   }
		   else if(card.getPos() == CardPosition.FACE_DOWN) {
			   System.out.print("<??> ");
		   }
		   else if(card.getPos() == CardPosition.FACE_UP) {
			   System.out.print("<" + card.getName() + "> ");
		   }
	   }
	   System.out.println();
	}
		
}
